<template>
	<div class="index">
		<!-- 轮播图区域 -->
		<div class="swiper">
			<swiper indicator-dots indicator-color='ivory' indicator-active-color='#d43c33' interval='3000' autoplay>
				<swiper-item v-for="(item,i) in asyncBannerList" :key="i">
					<image :src="item.pic" mode=""></image>
				</swiper-item>
			</swiper>
		</div>
		<!-- 导航 -->
		<nav class="nav">
			<div @click="toDailyRecommendation">
				<i class="iconfont icon-tuijian"></i>
				<span>每日推荐</span>
			</div>
			<div>
				<i class="iconfont icon-liebiao"></i>
				<span>歌单</span>
			</div>
			<div @click="toPaihangbang">
				<i class="iconfont icon-paixingbang"></i>
				<span>排行榜</span>
			</div>
			<div>
				<i class="iconfont icon-diantai"></i>
				<span>电台</span>
			</div>
<!-- 			<div>
				<i class="iconfont icon-zhiboxianxing"></i>
				<span>直播</span>
			</div> -->

		</nav>
		<div class="tuijian">
			<!-- 相关歌单详情 -->
			<title title="推荐歌单" :show="true" />
			<scroll-view scroll-x="true" class="scrollViewTuijian">
				<view class="scrollViewTuijianItem" v-for="item in asyncRecommendList" :key="item.id">
					<image :src="item.picUrl" @click="songList(item.id)"></image>
					<text class="more-lint-hidden">{{item.name}}</text>
					<view class="num">
						<i class="iconfont icon-erji">{{item.playCount}}次</i>
					</view>
				</view>
			</scroll-view>
		</div>
		<div class="tuijian mv">
			<!-- mv 推荐 -->
			<title title="推荐MV" :show="false" />
			<scroll-view scroll-x="true" class="scrollViewTuijian">
				<view class="scrollViewTuijianItem" v-for="item in mv" :key="item.id">
					<image :src="item.picUrl" @click="toMv(item.id)"></image>
					<text class="more-lint-hidden">{{item.name}}</text>
					<view class="num">
						<i class="iconfont icon-erji">{{item.playCount}}次</i>
					</view>
				</view>
			</scroll-view>
		</div>
		
		<!-- 华语 新歌 速递 -->
		<div class="newSongs">
			<title title="新歌速递 华语" :show="false" />
			<div class="main" v-if="newSongs[0].name">
				<div v-for="(item,index) in newSongs">
					<image :src="item.album.blurPicUrl" mode=""></image>
					<div class="main_info">
						<div class="main_info_name">{{item.name}}</div>
						<div class="main_info_desc">{{item.artists[0].name}}-{{item.album.name}}</div>
					</div>
					<div class="anniu" @click="toPlaySong(item.id)">
						<i class="iconfont icon-bofang"></i>
					</div>
				</div>
			</div>
		</div>
		<div class="bangdan">
			<title title="排行榜" :show="true" />
			<!-- 排行榜渲染 -->
			<scroll-view scroll-x="true" class="bangdanView">
				<view class="bangdanItem" v-for="(item,index) in topList" :key="item.name">
					<view class="title">
						<text>{{item.name}}</text>
						<i class="iconfont icon-gengduo"></i>
					</view>
					<view class="bangdanItemBox">
						<!-- tracks里面第一层的  id 获取 url -->
						<view class="bangdanItemBox-Item" v-for="(songs,index) in item.tracks" :key="songs.id">
							<image :src="songs.al.picUrl" mode=""></image>
							<text class="xuhao">{{index+1}}</text>
							<text class="one-lint-hidden name">{{songs.al.name}} - {{songs.ar[0].name}}</text>
							<i class="iconfont icon-bofang" @click="toSongPlay(songs.id)"></i>
						</view>
					</view>
				</view>
			</scroll-view>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				asyncBannerList: [], // 轮播图
				asyncRecommendList: [], // 推荐歌单
				topList: [], // 排行榜
				newSongs:[],// 华语新歌速递
				mv:[],// 推荐 的mv
			}
		},
		onLoad() {
			// 获取轮播图
			this.getBanner()
			// 获取推荐歌单
			this.getRecommendList()
			// 获取排行榜
			this.getTopList()
			// 获取  最新华语歌曲
			this.getNewSongsHuayu()
			// 获取 推荐 MV
			this.getMv()
		},
		methods: {
			// 排行榜的歌曲  去播放这个歌曲
			toSongPlay(id) {
				console.log('即将进入歌曲页的id',id);
				uni.navigateTo({
					url: '/subPackages/pages/songPlay/songPlay?id=' + id+'&type=index',

				})
			},
			// 获取 banner
			async getBanner() {
				try {
					const res = await uni.$request('get', '/banner?type=1')
					this.asyncBannerList = res.data.banners
					// 抛出错误
					// throw new Error('报错')
				} catch (e) {
					console.log('出错了');
					//TODO handle the exception
					uni.$utils.errorHander(e.message, 'error')
				}
			},
			// 推荐歌单
			async getRecommendList() {
				try {
					const res = await uni.$request('get', '/personalized', {
						limit: 15
					})
					res.data.result.forEach(item => {
						if (item.playCount > 10000) {
							return item.playCount = Math.round(item.playCount / 10000) + '万'
						} else {
							return item
						}
					})
					this.asyncRecommendList = res.data.result
				} catch (e) {
					//TODO handle the exception
					uni.$utils.errorHander(e.message, 'error')
				}
			},
			getTopList() {
				try {
					let list = []
					uni.$request('get', '/playlist/detail?id=19723756').then(res => {
						console.log(res.data);
						list.push({
							name: res.data.playlist.name,
							tracks: res.data.playlist.tracks.slice(0, 6),
							description: res.data.playlist.description,
							coverImgUrl: res.data.playlist.coverImgUrl
						})
						this.topList = list
					})
					uni.$request('get', '/playlist/detail?id=3779629').then(res => {
						console.log(res.data);
						list.push({
							name: res.data.playlist.name,
							tracks: res.data.playlist.tracks.slice(0, 6),
							description: res.data.playlist.description,
							coverImgUrl: res.data.playlist.coverImgUrl
						})
						this.topList = list
					})
					uni.$request('get', '/playlist/detail?id=2884035').then(res => {
						console.log(res.data);
						list.push({
							name: res.data.playlist.name,
							tracks: res.data.playlist.tracks.slice(0, 6),
							description: res.data.playlist.description,
							coverImgUrl: res.data.playlist.coverImgUrl
						})
						this.topList = list
					})
					uni.$request('get', '/playlist/detail?id=3778678').then(res => {
						console.log(res.data);
						list.push({
							name: res.data.playlist.name,
							tracks: res.data.playlist.tracks.slice(0, 6),
							description: res.data.playlist.description,
							coverImgUrl: res.data.playlist.coverImgUrl
						})
						this.topList = list
					})
					uni.$request('get', '/playlist/detail?id=5453912201').then(res => {
						console.log(res.data);
						list.push({
							name: res.data.playlist.name,
							tracks: res.data.playlist.tracks.slice(0, 6),
							description: res.data.playlist.description,
							coverImgUrl: res.data.playlist.coverImgUrl
						})
						this.topList = list
					})


				} catch (e) {
					//TODO handle the exception
					uni.$utils.errorHander(e.message, 'error')
				}
			},
			// 新歌速递  华语
			async getNewSongsHuayu() {
				try {
					const {data} = await uni.$request('get', '/top/song?type=7')
					if(data.code===200){
						this.newSongs = data.data.slice(0,10)
					}
					// 抛出错误
					// throw new Error('报错')
				} catch (e) {
					console.log('出错了');
					//TODO handle the exception
					uni.$utils.errorHander(e.message, 'error')
				}
			},
			// 推荐的 mv
			async getMv() {
				try {
					const {data} = await uni.$request('get', '/personalized/mv')
					if(data.code===200){
						this.mv = data.result
					}
					// 抛出错误
					// throw new Error('报错')
				} catch (e) {
					console.log('出错了');
					//TODO handle the exception
					uni.$utils.errorHander(e.message, 'error')
				}
			},			
			
			// 点击推荐歌单的图片 跳转到推荐歌单
			songList(id) {
				uni.navigateTo({
					url: '/pages/songList/songList?id=' + id,
				})
			},
			//  nav 部分的 点击跳转
			// 跳转到每日推荐页面
			toDailyRecommendation(){
				uni.navigateTo({
					url:'/subPackages/pages/dailyRecommendation/dailyRecommendation'
				})
			},
			// 跳转到 排行榜
			toPaihangbang(){
				uni.switchTab({
					url:'/pages/paihangbang/paihangbang'
				})
			},
			// 点击 新歌速度-华语 歌曲
			toPlaySong(id){
				uni.navigateTo({
					url:'/subPackages/pages/songPlay/songPlay?id='+id
				})
			},
			// 点击 mv 去 MV 的详情页
			toMv(id){
				uni.navigateTo({
					url:'/subPackages/pages/mv/mv?id='+id
				})
			}
		}
	}
</script>

<style lang="less">
	.index {
		background-color: #F8F8F8;

		// background-color: red;
		.swiper {
			height: 300rpx;

			image {
				height: 100%;
				width: 100%;
			}
		}

		.nav {
			padding: 20rpx 20rpx;
			display: flex;
			border-radius: 20rpx;
			background-color: #FFFFFF;

			>div {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				text-align: center;

				>i {
					width: 100rpx;
					height: 100rpx;
					line-height: 100rpx;
					font-size: 50rpx;
					border-radius: 50%;
					color: #fff;
					background-color: rgb(240, 19, 19);
				}

				>span {
					margin-top: 10rpx;
					text-align: center;
				}
			}
		}

		.tuijian {
			border-radius: 20rpx;
			background-color: #FFFFFF;
			padding: 0 20rpx 20rpx;
			margin-top: 20rpx;

			.scrollViewTuijian {
				white-space: nowrap;
				// display: flex;
				// justify-content: center;
				height: 300rpx;

				.scrollViewTuijianItem {
					position: relative;
					display: inline-block;
					width: 200rpx;
					height: 100%;
					margin-right: 20rpx;

					image {
						display: block;
						height: 200rpx;
						width: 200rpx;
						border-radius: 20rpx;
					}

					text {
						font-size: 26rpx;
						margin-top: 10rpx;
						width: 200rpx;
						height: 74rpx;
					}

					.num {
						position: absolute;
						right: 0;
						top: 0;
						height: 50rpx;
						line-height: 50rpx;
						width: 100%;
						background-color: rgba(0, 0, 0, .4);
						text-align: center;

						>i {
							color: #ccc;
							font-size: 28rpx;
						}
					}
				}
			}
		}
		.newSongs{
			border-radius: 20rpx;
			background-color: #FFFFFF;
			padding: 0 20rpx 20rpx;
			margin-top: 20rpx;		
			.main {
				padding: 40rpx;
			
				>div {
					display: flex;
					height: 170rpx;
					width: 100%;
					align-items: center;
			
					.main_info {
						// height: 100%;
						flex: 1;
						.main_info_name{
							color: #333333;
							font-size: 30rpx;
							
						}.main_info_desc{
							color: #acacac;
							font-size: 24rpx;
							
						}
					}
			
					.anniu {
						height: 100%;
						width: 80rpx;
						// background-color: red;
						text-align: center;
			
						i {
							color: #C0C0C0;
							font-size: 58rpx;
							line-height: 150rpx;
						}
					}
				}
			
				image {
					width: 150rpx;
					height: 150rpx;
					border-radius: 10rpx;
					margin-right: 20rpx;
				}
			}
		}
		.bangdan {
			border-radius: 20rpx;
			background-color: #FFFFFF;
			padding: 0 20rpx;
			margin-top: 20rpx;

			.bangdanView {
				white-space: nowrap;
				height: 1100rpx;

				.bangdanItem {
					display: inline-block;
					width: 100%;
					height: 1100rpx;
					// background-color: red;
					box-shadow: 0 15px 30px rgb(0 0 0 / 10%);
					border-radius: 20rpx;

					.title {
						display: flex;
						justify-content: center;
						align-items: center;
						font-size: 40rpx;
						margin-top: 10rpx;

						>i {
							font-size: 44rpx;
						}
					}

					.bangdanItemBox {
						padding: 20rpx;

						.bangdanItemBox-Item {
							display: flex;

							align-items: center;
							height: 150rpx;
							margin-bottom: 15rpx;

							image {
								width: 150rpx;
								height: 150rpx;
								border-radius: 10rpx;
							}

							.xuhao {
								font-size: 40rpx;
								font-style: italic;
								width: 100rpx;
								text-align: center;
							}

							.name {
								width: 350rpx;
							}

							>i {
								font-size: 40rpx;
								margin-left: 10rpx;
							}
						}
					}
				}
			}
		}
	}
</style>
